﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using CKS2320.Common.Utils;
using CKS2320.Common;

namespace CKS2320.Controller
{
    /// <summary>
    /// 基恩士激光扫描仪控制器
    /// </summary>
    public class KeyenceController
    {
        SocketUtils socket;
        readonly object obj = new object();
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public bool Connect(string ip, int port)
        {
            try
            {
                socket = new SocketUtils();
                return socket.Connect(ip, port);
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 向服务器发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public string Send(string msg)
        {
            lock (obj)
            {
                try
                {
                    socket.SendVarMessage(msg);
                    return socket.ReceiveMessage();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(" 发送数据出现异常!" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            return "";
        }
        /// <summary>
        /// 发送心跳包
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void SendBuffer()
        {
            lock (obj)
            {
                try
                {
                    socket.SendBbuffer();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(" 发送数据出现异常!" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }


   
        /// <summary>
        /// 监听 直到有数据返回
        /// </summary>
        /// <returns></returns>
        public string? ReceiveWhile(double timeOut = 0)
        {
            int len = 0;
            string receiveStr = string.Empty;
            DateTime startTime = DateTime.Now;
            while (len == 0)
            {
                receiveStr = socket.ReceiveMessage();
                if (!string.IsNullOrEmpty(receiveStr))
                {
                    len = receiveStr.Length;
                }
                #region timeout
                if (timeOut != 0)
                {
                    DateTime endTime = DateTime.Now;
                    double totalMilliseconds = (endTime - startTime).TotalMilliseconds;
                    if (totalMilliseconds > timeOut)
                    {
                        return KeyenceWhileResultEnum.timeout.ToString();
                    }
                }
                #endregion
            }
            return receiveStr;
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <returns></returns>
        public void Close()
        {
            try
            {
                socket.Close();
            }
            catch (Exception ex) { }
        }
        /// <summary>
        /// 监听通讯状态
        /// </summary>
        /// <returns></returns>
        public bool HandleSocket()
        {
            return socket.HandleSocket();
        }
    }
}
